/**
 * @author 橘柚
 * @Date 2025/3/26 14:17
 * @Description 计算商品购买时，根据所选套餐的优惠规则，确定最终的支付金额、到账数量和优惠金额
 */

interface ProductPackageResult {
  amountPayable: number; // 原始总价
  amountActual: number; // 实际支付金额
  discountPrice: number; // 优惠金额
  returnPrice: number; // 返现金额
  rechargeCount: number; // 购买数量
  actualCount: number; // 最终到账数量
  giftCount: number; // 赠送商品数量
}


/**
 * 计算购买商品时，参加套餐后的实际到账和优惠金额
 * @param price 商品单价 (支持 number 和 string)
 * @param num 购买数量 (支持 number 和 string)
 * @param productPackage 套餐信息（可选）
 * @returns 计算结果对象
 */
/**
 * @description 计算商品购买时，根据所选套餐的优惠规则，确定最终的支付金额、到账数量和优惠金额
 */
export const calculateProductPackage = (
  price: number | string,
  num: number | string,
  productPackage?: API.IProductPackage
) => {
  const parseNumber = (value: unknown, paramName: string): number => {
    if (typeof value === "number" && !isNaN(value)) return value;
    if (typeof value === "string" && value.trim() !== "") {
      const parsed = Number(value);
      if (!isNaN(parsed)) return parsed;
    }
    throw new Error(`参数 "${paramName}" 不是有效的数字: ${value}`);
  };

  const validPrice = parseNumber(price, "price");
  const validNum = parseNumber(num, "num");

  if (validPrice <= 0 || !Number.isFinite(validPrice)) {
    throw new Error(`参数 "price" 必须是大于 0 的有效数值: ${validPrice}`);
  }

  if (!Number.isInteger(validNum) || validNum <= 0) {
    throw new Error(`参数 "num" 必须是大于 0 的整数: ${validNum}`);
  }

  // 应付金额
  let amountPayable = validPrice * validNum;
  let amountActual = validPrice * validNum;
  let discountPrice = 0;
  let returnPrice = 0;
  let giftCount = 0;
  let actualCount = validNum;
  let rechargeCount = validNum;

  if (!productPackage || validNum < productPackage.minNum) {
    return {amountPayable, amountActual, discountPrice, returnPrice, actualCount, giftCount, rechargeCount};
  }

  // console.log("应付金额", amountPayable)
  // console.log("实付金额", amountActual)
  // console.log("优惠金额", discountPrice)
  // console.log("返现金额", returnPrice)
  // console.log("充值数量", rechargeCount)
  // console.log("赠送数量", giftCount)
  // console.log("实际数量", actualCount)

  switch (productPackage.type) {
    case "1": // 满减
      discountPrice = Math.floor(validNum / productPackage.minNum) * productPackage.reduceCash;
      amountActual = Math.max(amountPayable - discountPrice, 0);
      break;
    case "2": // 折扣
      amountActual = amountPayable * productPackage.discountRate;
      discountPrice = Math.max(amountPayable - amountActual, 0);
      break;
    case "3": // 充送
      giftCount = Math.floor(validNum / productPackage.minNum) * productPackage.giftNum;
      actualCount += giftCount;
      amountPayable = actualCount * validPrice;
      break;
    case "4": // 返现
      returnPrice = Math.floor(validNum / productPackage.minNum) * productPackage.returnCash;
      break;
    default:
      console.warn(`未知套餐类型: ${productPackage.type}`);
  }


  return {
    amountPayable,
    amountActual,
    discountPrice,
    returnPrice,
    actualCount,
    rechargeCount,
    giftCount,
  };
};
